package com.winderinfo.yikaotianxia.ui.order;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.core.api.Api;
import com.winderinfo.yikaotianxia.core.base.BaseActivity;
import com.winderinfo.yikaotianxia.core.mvp.Contract;
import com.winderinfo.yikaotianxia.core.mvp.Presenter;
import com.winderinfo.yikaotianxia.core.utils.ToastUtil;
import com.winderinfo.yikaotianxia.ui.register.RegisterBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OrderDetailsActivity extends BaseActivity implements Contract.View {
    @BindView(R.id.order_daizhifu)
    RelativeLayout orderDaizhifu;
    @BindView(R.id.order_orderStatus)
    TextView orderOrderStatus;
    @BindView(R.id.order_order)
    TextView orderOrder;
    @BindView(R.id.order_zhifufangshi)
    TextView orderZhifufangshi;
    @BindView(R.id.order_time)
    TextView orderTime;
    @BindView(R.id.order_name)
    TextView orderName;
    @BindView(R.id.order_phone)
    TextView orderPhone;
    @BindView(R.id.order_address)
    TextView orderAddress;
    @BindView(R.id.order_price)
    TextView orderPrice;
    @BindView(R.id.order_discount)
    TextView orderDiscount;
    @BindView(R.id.order_jianmian)
    RelativeLayout orderJianmian;
    @BindView(R.id.order_learn)
    TextView orderLearn;
    @BindView(R.id.order_xuexika)
    RelativeLayout orderXuexika;
    @BindView(R.id.order_coupons)
    TextView orderCoupons;
    @BindView(R.id.order_youhuiquan)
    RelativeLayout orderYouhuiquan;
    @BindView(R.id.order_shifu)
    TextView orderShifu;
    @BindView(R.id.order_zhifu)
    LinearLayout orderZhifu;
    @BindView(R.id.order_recycle)
    RecyclerView orderRecycle;
    @BindView(R.id.order_clane)
    TextView orderClane;
    @BindView(R.id.order_zf)
    TextView orderZf;
    @BindView(R.id.order_tixing)
    LinearLayout orderTixing;
    @BindView(R.id.order_shou)
    LinearLayout orderShou;
    private int orderID;

    private Presenter presenter;
    Map<String, Object> map = new HashMap<>();
    int id = 0;

    @Override
    protected int initLayoutId() {
        return R.layout.activity_orderdetails;
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        //订单Id
        orderID = intent.getIntExtra("orderID", 0);

        //实例P
        presenter = new Presenter(this);

        //布局管理器
        orderRecycle.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    protected void initData() {
        //请求数据
        /**
         * @id 订单ID
         * */
        map.clear();
        map.put("id", orderID);
        presenter.onPostRequest(Api.ORDERXIANGQIN, map, OrderInfo.class);

    }

    @OnClick({R.id.order_clane, R.id.order_zf, R.id.order_back, R.id.order_tixingfh, R.id.order_qshou})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.order_clane:
                break;
            case R.id.order_zf:
                break;
            case R.id.order_back:
                finish();
                break;
            case R.id.order_tixingfh:
                ToastUtil.showSuccess(this, "提醒发货成功");
                break;
            case R.id.order_qshou:
                //请求确认收货接口
                /**
                 * @id 订单Id
                 * */
                map.clear();
                map.put("id", id);
                presenter.onPostRequest(Api.SHOUHUO, map, RegisterBean.class);
                break;
        }
    }


    @Override
    public void onSuccess(Object data) {
        //详情数据
        if (data instanceof OrderInfo) {
            //订单id
             id = ((OrderInfo) data).getYkOrder().getId();
            //购买商品集合
            List<OrderInfo.YkOrderBean.YkGoodsBean> ykGoods = ((OrderInfo) data).getYkOrder().getYkGoods();
            CommodityAdapter commodityAdapter = new CommodityAdapter(this, ykGoods);
            orderRecycle.setAdapter(commodityAdapter);
            //判断状态
            if (((OrderInfo) data).getYkOrder().getOrderStatus().equals("0")) {//未支付显示时间
                orderDaizhifu.setVisibility(View.VISIBLE);
                orderZhifu.setVisibility(View.VISIBLE);
                orderOrderStatus.setText("待支付");
            } else if (((OrderInfo) data).getYkOrder().getOrderStatus().equals("1")) {//待发货
                orderOrderStatus.setText("待发货");
                orderTixing.setVisibility(View.VISIBLE);
            } else if (((OrderInfo) data).getYkOrder().getOrderStatus().equals("2")) {//待收货
                orderOrderStatus.setText("待收货");
                orderShou.setVisibility(View.VISIBLE);
            } else if (((OrderInfo) data).getYkOrder().getOrderStatus().equals("3")) {//已完成
                orderOrderStatus.setText("已完成");
            }
            orderOrder.setText("订单号：" + ((OrderInfo) data).getYkOrder().getOrderNo());
            if (((OrderInfo) data).getYkOrder().getPayType().equals("0")) {
                orderZhifufangshi.setText("支付方式：微信");
            }
            if (((OrderInfo) data).getYkOrder().getPayType().equals("1")) {
                orderZhifufangshi.setText("支付方式：支付宝");
            }
            orderTime.setText("下单时间: " + ((OrderInfo) data).getYkOrder().getCreateTime());
            orderName.setText("姓名：" + ((OrderInfo) data).getYkOrder().getUsername());
            orderPhone.setText("联系方式: " + ((OrderInfo) data).getYkOrder().getUserphone());
            orderAddress.setText("地址: " + ((OrderInfo) data).getYkOrder().getUseraddr());
            orderPrice.setText("￥ " + ((OrderInfo) data).getYkOrder().getPayMoney());
            //总价格
            int priceshi = ((OrderInfo) data).getYkOrder().getPayMoney();
            if (((OrderInfo) data).getYkOrder().getDiscount() > 0) {
                orderJianmian.setVisibility(View.VISIBLE);
                orderDiscount.setText("￥ -" + ((OrderInfo) data).getYkOrder().getDiscount());
                priceshi = priceshi - ((OrderInfo) data).getYkOrder().getDiscount();
            }
            if (((OrderInfo) data).getYkOrder().getLearn() > 0) {
                orderXuexika.setVisibility(View.VISIBLE);
                orderLearn.setText("￥ -" + ((OrderInfo) data).getYkOrder().getLearn());
                priceshi = priceshi - ((OrderInfo) data).getYkOrder().getLearn();
            }
            if (((OrderInfo) data).getYkOrder().getCoupons() > 0) {
                orderYouhuiquan.setVisibility(View.VISIBLE);
                orderCoupons.setText("￥ -" + ((OrderInfo) data).getYkOrder().getCoupons());
                priceshi = priceshi - ((OrderInfo) data).getYkOrder().getCoupons();
            }
            orderShifu.setText("￥" + priceshi);
        }
        //确认收货
        if (data instanceof RegisterBean) {
            if (((RegisterBean) data).getCode() == 0) {
                ToastUtil.showSuccess(this, ((RegisterBean) data).getMsg());
            } else {
                ToastUtil.showSuccess(this, ((RegisterBean) data).getMsg());
            }
        }
    }

    @Override
    public void onFailure(Object error) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.onUntying();
    }


}
